import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/widgets/dialog/xindong_base_sheet.dart';

class XinDongOptionsSheet extends StatelessWidget {
  final List dataList;
  final Function(int index)? onSelect;
  final bool showCancel;

  const XinDongOptionsSheet({Key? key, required this.dataList, this.onSelect, this.showCancel = true}) : super(key: key);

  XinDongOptionsSheet.show({super.key, required this.dataList, required this.onSelect, this.showCancel = true}) {
    showSmartSheet(sheet: this);
  }

  static Future<int?> showAwait({required List dataList, Function(int index)? onSelect, showCancel = true}) async {
    return await showSmartSheet(
      sheet: XinDongOptionsSheet(
        dataList: dataList,
        onSelect: onSelect,
        showCancel: showCancel,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        ListView.separated(
          padding: EdgeInsets.zero,
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemCount: showCancel ? dataList.length + 1 : dataList.length,
          itemBuilder: (BuildContext context, int index) {
            if (dataList.length == index) {
              return cancel();
            }
            return _buildItem(dataList[index], index);
          },
          separatorBuilder: (BuildContext context, int index) {
            return Container(color: DZColors.dividerColor, height: 1);
          },
        ),
      ],
    );
  }

  Widget _buildItem(String data, int index) {
    return GestureDetector(
      onTap: () {
        SmartDialog.dismiss(result: index);
        if (onSelect != null) {
          onSelect!(index);
        }
      },
      behavior: HitTestBehavior.opaque,
      child: Container(
        height: kButtonHeight,
        alignment: Alignment.center,
        child: Text(data, style: const TextStyle(color: DZColors.titleColor, fontSize: 16.0)),
      ),
    );
  }

  Widget cancel() {
    return GestureDetector(
      onTap: () {
        SmartDialog.dismiss();
      },
      behavior: HitTestBehavior.opaque,
      child: Container(
        height: kButtonHeight,
        alignment: Alignment.center,
        child: const Text("取消", style: TextStyle(color: DZColors.mainColor, fontSize: 16.0)),
      ),
    );
  }
}
